<?php
class user_manage {

	var $CI;
	public function __construct() {
		$this->CI =& get_instance();
	}

	/**
	 *
	 *
		* @name get_user_info
		* @date 2013-1-30
		* @author eifel
		* @param string 用户编号
		* @return array 用户详情
		* @version 1.0
		* @description 根据用户编号返回用户详情
	 */
	public function get_user_info($user_id) {
		$this->CI->load->model("common/adm_user_model");
		return $this->CI->adm_user_model->get_user_info($user_id);

	}


	/**
	 *
	 *
		* @name get_login_user_id
		* @date 2013-1-30
		* @author eifel
		* @param
		* @return string 登录的用户编号
		* @version 1.0
		* @description
	 */
	public function get_login_user_id() {
		$this->CI->load->library("session");
		return $this->CI->session->userdata("user_id");
	}


	/**
	 *
	 *
		* @name get_login_user_name
		* @date 2013-1-30
		* @author eifel
		* @param
		* @return string 登录的用户编号
		* @version 1.0
		* @description
	 */
	public function get_login_user_name() {
		$this->CI->load->library("session");
		return $this->CI->session->userdata("user_name");
	}

	/**
	 *
	 *
		* @name check_password
		* @date 2013-1-30
		* @author eifel
		* @param string 用户名,string 密码
		* @return boolean 检查支付结果
		* @version 1.0
		* @description 验证用户名和密码是否匹配
	 */
	public function check_password($user_name,$password) {
		$this->CI->load->model("common/adm_user_model");
		$user_login_info	=$this->CI->adm_user_model->check_login($user_name);
		if ($user_login_info['PASSWORD']!=md5($password)) {
			return false;
		}else{
			return true;
		}
	}

	public function change_password($user_name,$password) {

		$this->CI->load->model("common/adm_user_model");

		$this->CI->adm_user_model->change_password($user_name,md5($password));

	}
	public function check_user_name($user_name,$user_id) {
		if ($user_name=="") {
			$result['result']	=false;
			$result['message']	="用户名不能为空";
			return $result;
		}
		if (!preg_match("/^[a-za-z0-9\.\_]+$/", $user_name)) {
			$result['result']	=false;
			$result['message']	="用户名只能为英文、数字和下划线的组合";
			return $result;
		}
		if (strlen($user_name)>20||strlen($user_name)<4) {
			$result['result']	=false;
			$result['message']	="用户名的长度只能为4~16位";
			return $result;
		}
		$this->CI->load->model("common/adm_user_model");
		$user_info	=$this->CI->adm_user_model->query_user_by_name($user_name);
		if ($user_info==null) {
			$result['result']	=true;
			$result['message']	="用户名可以使用";
			return $result;
		}
		if ($user_info['user_id']==$user_id) {
			$result['result']	=true;
			$result['message']	="用户名可以使用";
			return $result;
		}else{
			$result['result']	=false;
			$result['message']	="用户名已经存在";
			return $result;
		}
	}

	/**
	 *
	 *
	 * @name check_email
	 * @date 2013-3-26
	 * @author eifel
	 * @param string 邮箱地址
	 * @return  array 校验结果
	 * @version 1.0
	 * @description 校验邮箱地址
	 */
	public function check_email($email) {
		if ($email=="") {
			$result['result']	=true;
			$result['message']	="";
			return $result;
		}
		if (!preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $email)) {
			$result['result']	=false;
			$result['message']	="邮箱地址的格式不正确";
			return $result;
		}

		$result['result']	=true;
		$result['message']	="邮箱地址填写正确";
		return $result;
	}
	/**
	 *
	 *
	 * @name check_name
	 * @date 2013-3-26
	 * @author eifel
	 * @param string 真实姓名
	 * @return array 校验结果
	 * @version 1.0
	 * @description 校验真实姓名是否正确
	 */
	public function check_name($name) {
		if ($name=="") {
			$result['result']	=false;
			$result['message']	="真实姓名不能为空";
			return $result;
		}
		if (mb_strlen($name,"utf-8")>20||mb_strlen($name,"utf-8")<2) {
			$result['result']	=false;
			$result['message']	="真实姓名的长度只能为2~20字";
			return $result;
		}
		$result['result']	=true;
		$result['message']	="真实姓名填写正确";
		return $result;
	}

	/**
	 *
	 *
	 * @name add_user
	 * @date 2013-3-26
	 * @author eifel
	 * @param array 用户信息
	 * @return  array 操作结果
	 * @version 1.0
	 * @description 添加用户并返回操作结果
	 */
	public function  add_user($user_data){
		$check_user_result 	=$this->check_user_info($user_data, "");
		if ($check_user_result['result']==false) {
			return $check_user_result;
		}
		$user_data['created_by']	=$_SESSION['user_name'];
		$user_data['created_time']	=date("Y-m-d H:i:s");
		$user_data['password']		=md5($user_data['password']);
		$this->CI->load->model("common/adm_user_model");
		$user_id	=$this->CI->adm_user_model->add_user($user_data);

		$this->add_user_role($user_id, $user_data['role_id']);
		$result['result']	=true;
		$result['redirect_url']	=base_url()."index.php/common_user_manage";
		$result['message']	="添加用户成功";
		return $result;

	}

	public function add_user_role($user_id,$role_id){

		$this->CI->load->model("common/adm_user_role_model");
		$this->CI->adm_user_role_model->delete_user_role($user_id);
		if ($role_id!="") {
			$this->CI->load->model("common/adm_user_role_model");
			foreach ($role_id as $ri){
				$this->CI->adm_user_role_model->add_user_role($user_id,$ri);
			}
		}
	}

	public function check_user_info($user_data,$user_id) {
		$check_user_name_result	=$this->check_user_name($user_data['user_name'], $user_id);
		if ($check_user_name_result['result']==false) {
			return $check_user_name_result;
		}
		$check_name_result	=$this->check_name($user_data['name']);
		if ($check_name_result['result']==false) {
			return $check_name_result;
		}
		$check_email_result	=$this->check_email($user_data['email']);
		if ($check_email_result['result']==false) {
			return $check_email_result;
		}
		if ($user_data['role_id']=="") {
			$result['result'] =false;
			$result['message']="你必须为用户选择一个角色";
			return $result;
		}
		if ($user_id=="") {
			if (strlen($user_data['password'])<5) {
				$result['result'] =false;
				$result['message']="密码的长度至少为5位";
				return $result;
			}
		}

		$result['result']	=true;
		$result['message']	="用户信息填写正确";
		return $result;
	}
	/**
	 *
	 *
	 * @name query_user_list
	 * @date 2013-3-26
	 * @author eifel
	 * @param
	 * @return array 用户列表
	 * @version 1.0
	 * @description 返回用户列表
	 */

	public function query_user_list() {
		$this->CI->load->model("common/adm_user_model");
		$start		=	intval($this->CI->input->get("per_page"));
		$limit		=	$this->CI->config->item("page_size");
		return	$this->CI->adm_user_model->query_user_list($start,$limit);
	}

	/**
	 *
	 *
	 * @name query_user_num
	 * @date 2013-3-26
	 * @author eifel
	 * @param
	 * @return int 用户的数量
	 * @version 1.0
	 * @description 返回用户的数量
	 */
	public function query_user_num() {
		$this->CI->load->model("common/adm_user_model");
		return $this->CI->adm_user_model->query_user_num();
	}
	/**
	 *
	 *
	 * @name query_user_detail
	 * @date 2013-3-26
	 * @author eifel
	 * @param string 用户编号
	 * @return array 用户信息
	 * @version 1.0
	 * @description 查询用户信息
	 */
	public function query_user_detail($user_id) {
		$this->CI->load->model("common/adm_user_model");
		return $this->CI->adm_user_model->query_user_detail($user_id);
	}

	/**
	 *
	 *
	 * @name modify_user
	 * @date 2013-3-27
	 * @author eifel
	 * @param array 用户信息, string 用户编号
	 * @return  array 操作结果
	 * @version 1.0
	 * @description 编辑用户信息
	 */
	public function modify_user($user_data,$user_id) {
		if ($user_id==1) {
			$result['result']	=false;
			$result['message']	="你不能编辑管理员";
			return $result;
		}

		$check_user_result 	=$this->check_user_info($user_data, $user_id);
		if ($check_user_result['result']==false) {
			return $check_user_result;
		}
		$user_data['modified_by']	=$_SESSION['user_name'];
		$user_data['modified_time']	=date("Y-m-d H:i:s");
		$this->CI->load->model("common/adm_user_model");
		$this->CI->adm_user_model->modify_user($user_data,$user_id);

		$this->add_user_role($user_id, $user_data['role_id']);
		$result['result']	=true;
		$result['redirect_url']	=base_url()."index.php/common_user_manage";
		$result['message']	="编辑用户成功";
		return $result;

	}

	/**
	 *
	 *
	 * @name delete_user
	 * @date 2013-3-27
	 * @author eifel
	 * @param string 用户编号
	 * @return
	 * @version 1.0
	 * @description 删除用户
	 */
	public function delete_user($user_id) {
		$this->CI->load->model("common/adm_user_model");
		$this->CI->adm_user_model->delete_user($user_id);
		$this->CI->load->model("common/adm_user_role_model");
		$this->CI->adm_user_role_model->delete_user_role($user_id);

	}

	/**
	 *
	 *
	 * @name search_user
	 * @date 2013-3-27
	 * @author eifel
	 * @param string 搜索用户
	 * @return array 用户列表
	 * @version 1.0
	 * @description 搜索用户
	 */
	public function search_user($key) {
		$this->CI->load->model("common/adm_user_model");
		return 	$this->CI->adm_user_model->search_user($key);
	}

	
	public function query_user_area($user_id){
		$this->CI->load->model("common/adm_user_region_model");
		
		return $this->CI->adm_user_region_model->query_user_region($user_id);
	}
	public function modify_user_area($user_id,$region_id){
		$this->CI->load->model("common/adm_user_region_model");
		$this->CI->adm_user_region_model->delete_user_region($user_id);
		if ($region_id) {
			foreach ($region_id as $region){
				$this->CI->adm_user_region_model->add_user_region($user_id,$region);
			}
		}
		$result['result']	=true;
		$result['redirect_url']	=base_url()."index.php/common_user_manage";
		$result['message']	="分配用户地区成功";
		return $result;
	}
}